import api from '@/api/index'
import request from '@/utils/request' // simulation data
// import formItemData from '@/views/jsonApi/formItem.js' // simulation data
import formItemData from '@/views/components/list.js' // simulation data
export default {
  install(Vue, options) {
    Vue.prototype.$api = api // api地址全局方法
    Vue.prototype.$request = request // api地址全局方法
    Vue.prototype.setScopeText = function(scope, type) {
      return scope.row[type]
    }
    // 成功全局方法
    Vue.prototype.successMessage = function(value) {
      this.$message({
        showClose: true,
        message: value,
        type: 'success',
        duration: 1000
      })
    }
    Vue.prototype.errorMessage = function(value) {
      this.$message({
        showClose: true,
        message: value,
        type: 'error',
        duration: 2500
      })
    }
    Vue.prototype.initComponentData = list => {
      if (!list) return
      const data = Array.isArray(list) ? list : [list]
      const value = {}
      const setArrVal = el => {
        if (el.name && !value[el.name]) {
          value[el.name] = el.category == 'checkbox' || el.category == 'cascader' ? [] : typeof (el.value) != 'undefined' ? el.value : ''
        }
      }
      const arr = data.map(e => {
        if (Array.isArray(e)) {
          e.forEach(es => setArrVal(es))
        } else {
          setArrVal(e)
        }
        return Object.assign({}, formItemData[e.category].config, e)
      })

      return {
        arr,
        value
      }
    }
  }

}
